// pages/store/bind.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cryptStr:'channelRole_0_I6091880620837d',
    loading:0,
    data:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      var scene = decodeURIComponent(options.scene);
      this.data.cryptStr = scene;
      wx.setStorageSync("backUrl", "/pages/store/bind?scene=" + scene);
    }
    
    var that = this;
    app.getUserInfo(function(data){
      that.setData({ userInfo: data});
      that.findByCrypt();
      wx.removeStorageSync("backUrl");
    })
   
  },
  findByCrypt:function(){
    var that = this;
    if (this.data.loading == 1) {
      return false;
    }
    this.data.loading = 1;
    console.log(this.data);
    app.api("api/community/findByCrypt", {
      cryptStr: this.data.cryptStr,
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      } 
      that.setData({ data: res.data });
    })
  },
  bind:function(){
    var that = this;
    if (this.data.loading == 1) {
      return false;
    }
    //this.data.loading = 1;
    this.setData({ disabled: 1, loading:1})
    console.log(this.data);
    app.api("api/user/addStoreUser", {
      cryptStr: this.data.cryptStr,
    }, function (res) {
      that.setData({ disabled: 0, loading:0 })
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      wx.showToast({
        title: '绑定成功！',
        icon: 'success',
        duration: 2000
      })
      that.setData({ data:res.data});
      wx.switchTab({
        url: '/pages/index/index',
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})